package com.example.gmr.Activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.icu.text.IDNA;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toast;

import com.example.gmr.Adapter.BeautyAdapter;
import com.example.gmr.Entity.Beauty;
import com.example.gmr.Helper.DatabaseHelper;
import com.example.gmr.R;

import java.util.LinkedList;

public class Login extends AppCompatActivity implements AdapterView.OnItemClickListener {

    private ListView listView;
    private LinkedList<Beauty> linkedList;
    private BeautyAdapter beautyAdapter;
    private Context context;
    private DatabaseHelper databaseHelper;
    private SQLiteDatabase db;
    private XXCM xxcm;
    private SHANG shang;
    private HULI huli;
    private WGY wgy;
    private SearchView searchView;
    //private final String[] strings = {"aaa","bbb","ccc"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        searchView=(SearchView)findViewById(R.id.searchView);
        listView=(ListView)findViewById(R.id.listView);

        //listView.setAdapter(new ArrayAdapter<>(this , android.R.layout.simple_dropdown_item_1line , strings));
//        listView.setTextFilterEnabled(true);    //listview启动过滤
//        listView.setVisibility(View.GONE);     //一开始不显示
//        searchView.setIconifiedByDefault(false);     //搜索框不自动缩小为一个搜索图标，而是match_parent
//        searchView.setSubmitButtonEnabled(true);     //显示搜索按钮
//
//        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
//            @Override
//            public boolean onQueryTextSubmit(String query) {    //单击搜索按钮的监听
//                return false;
//            }
//            @Override
//            public boolean onQueryTextChange(String newText) {    //输入字符的监听
//                if (TextUtils.isEmpty(newText)){
//                    listView.setVisibility(View.GONE);
//                    listView.clearTextFilter();
//                }
//                else {
//                    listView.setVisibility(View.VISIBLE);
//                    listView.setFilterText(newText);
//                }
//                return true;
//            }
//        });

        context=Login.this;
        linkedList=new LinkedList<Beauty>();
        databaseHelper=new DatabaseHelper(this,"user",null,1);
        db=databaseHelper.getWritableDatabase();
        Cursor cursor=db.rawQuery("select * from user",null);
        if (cursor==null){
            Toast.makeText(context,"没有数据",Toast.LENGTH_SHORT).show();
        }else {
            while (cursor.moveToNext()){
                String name=cursor.getString(cursor.getColumnIndex("name"));
                String school=cursor.getString(cursor.getColumnIndex("school"));
                String grade=cursor.getString(cursor.getColumnIndex("grade"));
                String touxiang=cursor.getString(cursor.getColumnIndex("touxiang"));
                Beauty beauty=new Beauty(name,school,grade,touxiang);
                linkedList.add(beauty);
            }
            cursor.close();
            beautyAdapter=new BeautyAdapter(linkedList,context);
            listView.setAdapter(beautyAdapter);
            listView.setOnItemClickListener(this);
        }
        registerForContextMenu(listView);

        //获取内部类对象
        xxcm=new XXCM();
        IntentFilter intentFilter1=new IntentFilter();
        intentFilter1.addAction("MY_ACTION");
        registerReceiver(xxcm,intentFilter1);

        shang=new SHANG();
        IntentFilter intentFilter2=new IntentFilter();
        intentFilter2.addAction("MY_ACTION");
        registerReceiver(shang,intentFilter2);

        huli=new HULI();
        IntentFilter intentFilter3=new IntentFilter();
        intentFilter3.addAction("MY_ACTION");
        registerReceiver(huli,intentFilter3);

        wgy=new WGY();
        IntentFilter intentFilter4=new IntentFilter();
        intentFilter4.addAction("MY_ACTION");
        registerReceiver(wgy,intentFilter4);
    }

    //列表点击事件
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long id) {
        switch (i){
            case 0:
                Intent intent=new Intent(context,Information.class);
                Beauty data=linkedList.get(i);
                intent.putExtra("name",data.getName());
                intent.putExtra("school",data.getSchool());
                intent.putExtra("grade",data.getGrade());
                intent.putExtra("touxiang",data.getTouxiang());
                Toast.makeText(context,data.getName().toString(),Toast.LENGTH_SHORT).show();
                startActivity(intent);
                break;
            case 1:
                //添加通知栏
                NotificationCompat.Builder notification=new NotificationCompat.Builder(context);
                notification.setSmallIcon(R.drawable.logo);
                notification.setContentTitle("银能选美");
                notification.setContentText("您正在查看选手信息");
                notification.setDefaults(Notification.DEFAULT_ALL);
                Notification n=notification.build();
                NotificationManager manager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
                manager.notify(1,n);
                break;
            default :
                break;
        }
    }

    //菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(Menu.NONE,1,1,"添加选手信息");
        menu.add(Menu.NONE,2,2,"益智小游戏");
        menu.add(Menu.NONE,3,3,"音乐播放器");
        menu.add(Menu.NONE,4,4,"发送广播");
        menu.add(Menu.NONE,5,5,"退出账号");
        return super.onCreateOptionsMenu(menu);
    }

    //菜单点击事件
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        final Intent intent;
        int id=item.getItemId();
        switch (id){
            case 1:
                intent=new Intent(context,Add_User.class);
                startActivity(intent);
                break;
            case 2:
                intent=new Intent(context,Mouse.class);
                startActivity(intent);
                break;
            case 3:
                intent=new Intent(context,SongList.class);
                startActivity(intent);
                break;
            case 4:
                SendBroadCast();
                break;
            case 5:
                AlertDialog.Builder alertDialog=new AlertDialog.Builder(context);
                alertDialog.setIcon(R.drawable.logo);
                alertDialog.setTitle("退出");
                alertDialog.setMessage("您确定要退出银能选美");
                alertDialog.setPositiveButton("确定",new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent=new Intent(context,Main.class);
                        startActivity(intent);
                        finish();
                    }
                });
                alertDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                alertDialog.create();
                alertDialog.show();
                break;
            default :
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    //上下文菜单
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        menu.add(Menu.NONE,0,1,"删除");
        menu.add(Menu.NONE,1,2,"编辑");
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    //上下文菜单点击事件
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        final int id=item.getItemId();
        switch (id){
            case 0:
                final AlertDialog.Builder shanchu=new AlertDialog.Builder(context);
                shanchu.setIcon(R.drawable.logo);
                shanchu.setTitle("删除警告");
                shanchu.setMessage("您确定要删除该选手信息");
                shanchu.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        databaseHelper=new DatabaseHelper(context,"user",null,1);
                        db=databaseHelper.getWritableDatabase();
                        db.delete("user","name=?",new String[]{linkedList.get(id).getName()});
                        linkedList.remove(id);
                        beautyAdapter.notifyDataSetInvalidated();
                        Toast.makeText(context," 已删除",Toast.LENGTH_SHORT).show();
                    }
                });
                shanchu.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                shanchu.create();
                shanchu.show();
                break;
            case 1:
                Intent intent=new Intent(Login.this,Updata_User.class);
                startActivity(intent);
        }

        return super.onContextItemSelected(item);
    }

    //发送广播
    public void SendBroadCast(){
        Intent intent=new Intent();
        intent.setAction("MY_ACTION");
        intent.putExtra("data","银川能源学院银能选美主办方发");
        sendOrderedBroadcast(intent,null,null,null,1,"10000",null);
    }

    //接收广播内部类
    class XXCM extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context,"信息传媒学院已收到!",Toast.LENGTH_SHORT).show();
        }
    }
    class SHANG extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context,"商学院已收到!",Toast.LENGTH_SHORT).show();
        }
    }
    class HULI extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context,"护理学院已收到!",Toast.LENGTH_SHORT).show();
        }
    }
    class WGY extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context,"外国语学院已收到!",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (xxcm!=null){
            unregisterReceiver(xxcm);
        }
        if (shang!=null){
            unregisterReceiver(shang);
        }
        if (huli!=null){
            unregisterReceiver(huli);
        }
        if (wgy!=null){
            unregisterReceiver(wgy);
        }
    }
}
